﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using ODS.Utils;
using ODS.WebService.DeviceCommand;
using ODS.WebService.DeviceData;
using ODS.Model;
using ODS.Config;

namespace ODS.UserControls
{
    public partial class GetLocationPage : UserControl
    {
        public DetailDevicePage ParentView;

        //MessageModel lastestMessage;

        public GetLocationPage()
        {
            InitializeComponent();
        }


        private async void btn_getLocation_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            btn_getLocation.IsEnabled = false; 

            String errorMessage = null;

            InfoResult result_requestCommand = await RequestCommandService.RequestCommand(App.currentDeviceModel.DeviceIMEI, 11, null);
            if (result_requestCommand.IsSuccess)
            {
                if ((String)result_requestCommand.ReturnedObject == Constants.SUCCESS)
                {
                    MessageBox.Show("Location requested!");
                    //InfoResult result_getLastestMessage = await RetrieveDeviceLatestMessageService.RetrieveDeviceLatestMessage(App.currentDeviceModel.DeviceIMEI);
                    //if (result_getLastestMessage.IsSuccess)
                    //{
                    //    if (result_getLastestMessage.ReturnedObject != null)
                    //    {
                    //        lastestMessage = result_getLastestMessage.ReturnedObject as MessageModel;

                    //        GoogleServiceHandler googleService = new GoogleServiceHandler();
                    //        googleService.OnGetGeocodingLocation += googleService_OnGetGeocodingLocation;
                    //        googleService.GetPlaceByLatLng(Double.Parse(lastestMessage.Latitude), Double.Parse(lastestMessage.Longitude));
                    //    }
                    //    else
                    //    {
                    //        errorMessage = result_getLastestMessage.Error;

                    //    }
                    //}
                    //else
                    //{
                    //    errorMessage = "Request processing error. Please try again!";
                    //}
                }
                else
                {
                    MessageBox.Show((String) result_requestCommand.ReturnedObject);
                }
            }
            else
            {
                errorMessage = result_requestCommand.Error;
            }

            if (errorMessage != null)
            {
                MessageBox.Show(errorMessage);
            }

            btn_getLocation.IsEnabled = true;
        }

    }
}
